﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using King.Engine.Content;
using King.Engine.Core;

namespace King.Engine.GameObjects
{

    /// <summary>
    /// A game object with a single sprite
    /// </summary>
    public abstract class SimpleSpriteObject : GameObject
    {

        #region Fields

        protected Texture2D _texture;
        private float _drawDepth;

        #endregion


        #region Constructor

        protected SimpleSpriteObject(string spriteName, float drawDepth)
        {
            _drawDepth = drawDepth;
            ChangeSprite(spriteName);
        }

        internal SimpleSpriteObject()
        {

        }

        #endregion


        #region Public Methods

        /// <summary>
        /// Change the sprite to a new sprite of the given name
        /// </summary>
        public void ChangeSprite(string spriteName)
        {
            _texture = ContentLoader.Get<Texture2D>(spriteName);
        }

        protected override void DrawObject()
        {
            Screen.Draw(this.IsGUI, _texture, this.Bounds, _drawDepth);
        }

        #endregion
    }

    /// <summary>
    /// A game object that supports multiple sprites
    /// </summary>
    /// <typeparam name="T">Each sprite is keyed by a value of type T. This should be an enum</typeparam>
    public abstract class SpriteObject<T> : SimpleSpriteObject
    {

        #region Fields

        public Dictionary<T, Texture2D> _sprites;

        #endregion


        #region Constructor

        /// <summary>
        /// Create a SpriteObject
        /// </summary>
        /// <param name="_spriteNames">Dictionary that maps T values to sprite names</param>
        /// <param name="drawDepth">The draw depth of the sprites</param>
        public SpriteObject(Dictionary<T, string> _spriteNames, float drawDepth)
        {
            _sprites = _spriteNames.ToDictionary(kv => kv.Key, 
                                                 kv => ContentLoader.Get<Texture2D>(kv.Value));
        }
        
        #endregion


        #region Public Methods

        /// <summary>
        /// Set the sprite from the given key
        /// </summary>
        public void SetSprite(T sprite)
        {
            _texture = _sprites[sprite];
        }

        #endregion

    }
}
